package com.zzf.concurrent.example.graceful.interrupt;

import com.zzf.concurrent.example.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author GaoFeng2017
 * @date 2019/8/5 20:00
 */
@Slf4j
public class InterruptExample {

    private static final Object MONITOR = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while(true) {
                // TimeUtil.sleep(1000);
                // synchronized (MONITOR) {
                    /*try {
                        MONITOR.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        log.error("线程已被中断，{}", Thread.currentThread().isInterrupted());
                        System.exit(0);
                    }*/
                    /*log.info("正在通信中：{}, {}, {}", Thread.currentThread().getName(),
                            System.currentTimeMillis(), Thread.currentThread().isInterrupted());*/
                // }
            }
        });

        Thread mainThread = Thread.currentThread();
        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mainThread.interrupt();
        });

        t1.start();
        t2.start();

        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("线程已被中断, join，{}", Thread.currentThread().isInterrupted());
            System.exit(0);
        }

        TimeUtil.sleep(1000);
        t1.interrupt();

        log.info("interrupt");
    }
}
